<?php
/**用户管理*/
namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    /**
     *发送验证码
     * @return json
     *200  发送成功
     *201  发送失败
     *@type 【1=>注册,2=>密码找回，3=>修改密码】
     */
    public function SendCode(Request $request){
        $phone = $request->input('phone');
        if(empty($phone)){
            $data = ['code'=>'1010','messages'=>"phone字段不能为空"];
            return response()->json($data);
        }
        $type = $request->input('type');
        if(empty($type)){
            $data = ['code'=>'1010','messages'=>"type字段不能为空"];
            return response()->json($data);
        }
        if($type=="1"){
            $num=Db::table('frontusers')->where("phone",$phone)->count();
            if($num > 0){
                $data["code"]="400";
                $data["messages"]="该手机号已注册";
                return response()->json($data);
            }
        }
        $res=$this->CommonSendCode($phone,$type);
        return response()->json($res);
    }

    /*忘记密码
    * @return json
    *200  注册成功返回用户信息
    *5001 字段不能为空
    *201  重置密码失败
    */
    function ForgetUserPasswd(Request $request){
        $user_id = $request->input('user_id');
        if(empty($user_id)){
            $data = ['code'=>'1010','messages'=>"user_id字段不能为空"];
            return response()->json($data);
        }
        $password = $request->input('password');
        if(empty($password)){
            $data = ['code'=>'1010','messages'=>"password字段不能为空"];
            return response()->json($data);
        }
        if(strlen($password) < 6){
            $data = ['code'=>'506','messages'=>"新密码长度不能小于6位"];
            return response()->json($data);
        }
        $base["password"]=Hash::make($password);
        $r=DB::table('frontusers')->where("id",$user_id)->update($base);
        if($r){
            $data = ['code'=>'200','messages'=>"重置密码成功"];
            return response()->json($data);
        }else{
            $data = ['code'=>'201','messages'=>"重置密码失败"];
            return response()->json($data);
        }
    }
    /**
     * 修改密码
     * @return json
     *200  注册成功返回用户信息
     *5001 字段不能为空
     *201  修改密码失败
     */
    function SaveUserPasswd(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"id字段不能为空"];
            return response()->json($data);
        }
        $old_pass = $request->input('old_pass');
        if(empty($old_pass)){
            $data = ['code'=>'5001','messages'=>"old_pass字段不能为空"];
            return response()->json($data);
        }
        $new_pass = $request->input('new_pass');
        if(empty($new_pass)){
            $data = ['code'=>'5001','messages'=>"new_pass字段不能为空"];
            return response()->json($data);
        }
        $password=DB::table('frontusers')->where("id",$user_id)->value("password");
        if(!Hash::check($old_pass,$password)){
            $data = ['code'=>'5005','messages'=>"旧密码错误"];
            return response()->json($data);
        }
        if(strlen($new_pass) < 6){
            $data = ['code'=>'5006','messages'=>"新密码长度不能小于6位"];
            return response()->json($data);
        }

        $base["password"]=Hash::make($new_pass);
        $r=DB::table('frontusers')->where("id",$user_id)->update($base);
        if($r){
            $data = ['code'=>'200','messages'=>"修改密码成功"];
            return response()->json($data);
        }else{
            $data = ['code'=>'201','messages'=>"修改密码失败"];
            return response()->json($data);
        }
    }
    /*
     * 修改手机号
     * @return json
     * 200 注册成功返回用户信息
     * 50001 字段不能为空
     * 201 修改密码失败
     * */
    function SaveUserPhone(Request $request){
        $user_id = $request->input('user_id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"id字段不能为空"];
            return response()->json($data);
        }
        $phone = $request->input('phone');
        if(empty($phone)){
            $data = ['code'=>'5001','messages'=>"手机号不能为空"];
            return response()->json($data);
        }
        $new_phone = $request->input('new_phone');
        if(empty($new_phone)){
            $data = ['code'=>'5001','messages'=>"请输入新手机号"];
            return response()->json($data);
        }
        if(strlen($new_phone) < 11){
            $data = ['code'=>'5006','messages'=>"请输入正确的手机号码"];
            return response()->json($data);
        }
        $code = $request->input('code');
        /**d短信验证码校验*/
        $param=[
            'mscode' => trim($code)
        ];
        if(isset($_COOKIE['phonemsg'])){
            if($param['mscode'] != $_COOKIE['phonemsg'])
                $data = ['code'=>'5001','messages'=>"验证码错误，请重新获取"];
                return response()->json($data);
        }else{
            $data = ['code'=>'5001','messages'=>"验证码已过期，请重新获取"];
            return response()->json($data);
        }
        $base["phone"]=$new_phone;
        $r=DB::table('frontusers')->where("id",$user_id)->update($base);
        if($r){
            $data = ['code'=>'200','messages'=>"修改手机成功"];
            return response()->json($data);
        }else{
            $data = ['code'=>'201','messages'=>"修改手机失败"];
            return response()->json($data);
        }
    }
    /**
     * 修改昵称
     * @return json
     *5001 字段不能为空
     *201  修改失败
     */
    function SaveUserNickName(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"id字段不能为空"];
            return response()->json($data);
        }
        $nickname = $request->input('nickname');
        if(empty($nickname)){
            $data = ['code'=>'5001','messages'=>"nickname字段不能为空"];
            return response()->json($data);
        }

        $base["name"]=$nickname;
        $r=DB::table('frontusers')->where("id",$user_id)->update($base);
        if($r){
            $data = ['code'=>'200','messages'=>"修改昵称成功"];
            return response()->json($data);
        }else{
            $data = ['code'=>'201','messages'=>"修改昵称失败"];
            return response()->json($data);
        }
    }

    /**
     * 修改邮箱
     * @return json
     *5001 字段不能为空
     *201  修改失败
     */
    function SaveUserEmail(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"id字段不能为空"];
            return response()->json($data);
        }
        $email = $request->input('email');
        if(empty($email)){
            $data = ['code'=>'5001','messages'=>"email字段不能为空"];
            return response()->json($data);
        }
        $mode = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';
        if(!preg_match($mode,$email)){
            $data = ['code'=>'5006','messages'=>"请输入正确的邮箱地址"];
            return response()->json($data);
        }
        $email_num=DB::table('frontusers')->where("email",$email)->count();
        if($email_num > 0){
            $data = ['code'=>'5008','messages'=>"邮箱已被使用,请更换"];
            return response()->json($data);
        }

        $base["email"]=$email;
        $r=DB::table('frontusers')->where("id",$user_id)->update($base);
        if($r){
            $data = ['code'=>'200','messages'=>"修改邮箱成功"];
            return response()->json($data);
        }else{
            $data = ['code'=>'201','messages'=>"修改邮箱失败"];
            return response()->json($data);
        }
    }

    /**
     * 修改头像
     * @return json
     *5001 字段不能为空
     *201  修改失败
     */
    function SaveUserAvator(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"user_id字段不能为空"];
            return response()->json($data);
        }
        if (!$request->hasFile('avatar')) {
            $data = ['code'=>'5001','messages'=>"没有上传文件"];
            return response()->json($data);
        }
        //获取文件扩展名
        $extension = $request->avatar->extension();
        $img_range = ['jpg','jpeg','png'];
        if(!in_array($extension,$img_range)){
            $data = ['code'=>'201','messages'=>"图片上传失败只能是jpg,jpeg,png格式"];
            return response()->json($data);
        }
        $file = $request->file('avatar');
        $destinationPath = './upload/avatar/'.date('Ym',time());
        $re_imgname = str_pad(rand(000000,999999),'6','0',STR_PAD_LEFT ).date('YmdHis',time()).".".$extension; //文件重命名
        $file->move($destinationPath,$re_imgname);
        $base["avator"]=$destinationPath."/".$re_imgname;
        $r=DB::table('frontusers')->where("id",$user_id)->update($base);
        if($r){
            $data = ['code'=>'200','messages'=>"修改头像成功"];
            return response()->json($data);
        }else{
            $data = ['code'=>'201','messages'=>"修改头像失败"];
            return response()->json($data);
        }
    }
    /**
     * 提升额度审核状态
     * @return json
     *5001 字段不能为空
     *201  修改失败
     */
    function ApplyQuotaMessageCheckStatus(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"user_id字段不能为空"];
            return response()->json($data);
        }
        $applyquota_list = DB::table('applyquota_message')->where('userid',$user_id)->first();
        if(empty($applyquota_list)){
            $data = ['code'=>'200','messages'=>"等待提交信息"];
            return response()->json($data);
        }else{
            if($applyquota_list->status=="0"){
                $data = ['code'=>'5009','messages'=>"正在审核中"];
                return response()->json($data);
            }
            if($applyquota_list->status=="1"){
                $data = ['code'=>'5010','messages'=>"审核通过"];
                return response()->json($data);
            }
        }
    }
    /**
     * 提升额度 type 1 添加 2 更新
     * @return json
     *5001 字段不能为空
     *201  修改失败
     */
    function ApplyQuotaMessage(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"user_id字段不能为空"];
            return response()->json($data);
        }
        $type = $request->input('type');
        if(empty($type)){
            $data = ['code'=>'5001','messages'=>"type字段不能为空"];
            return response()->json($data);
        }
        //公司名称
        $company_name = $request->input('company_name');
        if(empty($company_name)){
            $data = ['code'=>'5001','messages'=>"company_name字段不能为空"];
            return response()->json($data);
        }
        //公司电话
        $company_phone = $request->input('company_phone');
        if(empty($company_phone)){
            $data = ['code'=>'5001','messages'=>"company_phone字段不能为空"];
            return response()->json($data);
        }
        //办公地址
        $company_addr = $request->input('company_addr');
        if(empty($company_addr)){
            $data = ['code'=>'5001','messages'=>"company_addr字段不能为空"];
            return response()->json($data);
        }
        //详细地址
        $company_addr_dt = $request->input('company_addr_dt');
        if(empty($company_addr_dt)){
            $data = ['code'=>'5001','messages'=>"company_addr_dt字段不能为空"];
            return response()->json($data);
        }
        //统一社会信用码
        $socialcredit = $request->input('socialcredit');
        if(empty($socialcredit)){
            $data = ['code'=>'5001','messages'=>"socialcredit字段不能为空"];
            return response()->json($data);
        }
        //公司股东或法人
        $username = $request->input('username');
        if(empty($socialcredit)){
            $data = ['code'=>'5001','messages'=>"username字段不能为空"];
            return response()->json($data);
        }
        //股东或法人联系方式
        $userphone = $request->input('userphone');
        if(empty($userphone)){
            $data = ['code'=>'5001','messages'=>"userphone字段不能为空"];
            return response()->json($data);
        }
        //身份证号码
        $identificatiomn = $request->input('identificatiomn');
        if(empty($identificatiomn)){
            $data = ['code'=>'5001','messages'=>"identificatiomn字段不能为空"];
            return response()->json($data);
        }
        //公司营业执照图片
        if (!$request->hasFile('company_img')) {
            $data = ['code'=>'5001','messages'=>"公司营业执照图片必传"];
            return response()->json($data);
        }
        //身份证正面图片地址
        if (!$request->hasFile('ident_face')) {
            $data = ['code'=>'5001','messages'=>"身份证正面图片必传"];
            return response()->json($data);
        }
        //身份证反面图片
        if (!$request->hasFile('ident_backface')) {
            $data = ['code'=>'5001','messages'=>"身份证反面图片必传"];
            return response()->json($data);
        }
        //获取文件扩展名
        $company_img_extension = $request->company_img->extension();
        $img_range = ['jpg','jpeg','png'];
        if(!in_array($company_img_extension,$img_range)){
            $data = ['code'=>'201','messages'=>"图片上传失败只能是jpg,jpeg,png格式"];
            return response()->json($data);
        }
        $ident_face_extension = $request->ident_face->extension();
        if(!in_array($ident_face_extension,$img_range)){
            $data = ['code'=>'201','messages'=>"图片上传失败只能是jpg,jpeg,png格式"];
            return response()->json($data);
        }
        $ident_backface_extension = $request->ident_backface->extension();
        if(!in_array($ident_backface_extension,$img_range)){
            $data = ['code'=>'201','messages'=>"图片上传失败只能是jpg,jpeg,png格式"];
            return response()->json($data);
        }

        $destinationPath = './upload/quota_img/'.date('Ym',time());
        $company_img_file = $request->file('company_img');
        $company_img_re_imgname = str_pad(rand(000000,999999),'6','0',STR_PAD_LEFT ).date('YmdHis',time()).".".$company_img_extension; //文件重命名
        $company_img_file->move($destinationPath,$company_img_re_imgname);

        $ident_face_file = $request->file('ident_face');
        $ident_face_re_imgname = str_pad(rand(000000,999999),'6','0',STR_PAD_LEFT ).date('YmdHis',time()).".".$ident_face_extension; //文件重命名
        $ident_face_file->move($destinationPath,$ident_face_re_imgname);

        $ident_backface_file = $request->file('ident_backface');
        $ident_backface_re_imgname = str_pad(rand(000000,999999),'6','0',STR_PAD_LEFT ).date('YmdHis',time()).".".$ident_backface_extension; //文件重命名
        $ident_backface_file->move($destinationPath,$ident_backface_re_imgname);

        $database["company_name"]=$company_name;
        $database["company_phone"]=$company_phone;
        $database["company_addr"]=$company_addr;
        $database["company_addr_dt"]=$company_addr_dt;
        $database["socialcredit"]=$socialcredit;
        $database["company_img"]=$destinationPath."/".$company_img_re_imgname;
        $database["username"]=$username;
        $database["userphone"]=$userphone;
        $database["identificatiomn"]=$identificatiomn;
        $database["ident_face"]=$destinationPath."/".$ident_face_re_imgname;
        $database["ident_backface"]=$destinationPath."/".$ident_backface_re_imgname;
        $database["status"]=0;
        $database["created_at"]=date('Y-m-d H:i:s',time());
        $database["updated_at"]=date('Y-m-d H:i:s',time());
        if($type=="1"){
            $database["userid"]=$user_id;
            $res = DB::table('applyquota_message')->insertGetId($database);
            $quota_data = [
                'quota_user_id'=>$user_id,
                'quota_company_name'=>$database['company_name'],
                'quota_amount'=>0,
                'quota_current'=>0,
                'quota_used'=>0,
                'quota_zhima'=>0,
                'quota_isdel'=>0,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time())
            ];
            $quota = DB::table('quota')->insertGetId($quota_data);
            if($res > 0){
                $data = ['code'=>'200','messages'=>"提交信息成功"];
                return response()->json($data);
            }else{
                $data = ['code'=>'201','messages'=>"提交信息失败"];
                return response()->json($data);
            }
        }else{
            $res = DB::table('applyquota_message')->where("userid",$user_id)->update($database);
            if($res){
                $data = ['code'=>'200','messages'=>"更新信息成功"];
                return response()->json($data);
            }else{
                $data = ['code'=>'201','messages'=>"更新信息失败"];
                return response()->json($data);
            }
        }
    }
    /**
     * 个人信息
     * @return json
     *5001 字段不能为空
     *201  修改失败
     */
    function PersonInfo(Request $request){
        $user_id = $request->input('id');
        if(empty($user_id)){
            $data = ['code'=>'5001','messages'=>"id字段不能为空"];
            return response()->json($data);
        }
        $user_list = DB::table('frontusers')->where('id',$user_id)->first();
        $user_list = [
            'id' => $user_list->id,
            'avator' => env('APP_WEB_PATH').$user_list->avator,
            'name' => $user_list->name,
            'password' => $user_list->password,
            'email' =>$user_list->email,
            'phone' =>$user_list->phone,
            'account' => $user_list->account,
            'leve' => $user_list->leve,
            'status' => $user_list->status
        ];
        if(empty($user_list)){
            $data = ['code'=>'5004','messages'=>"无此用户"];
            return response()->json($data);
        }else{
            $data = ['code'=>'200','result'=>$user_list];
            return response()->json($data);
        }
    }





}
